def tag(name, *content, cls=None, **attrs):
    """生成一个或多个HTML标签"""
    if cls is not None:
        attrs['class'] = cls
    if attrs:
        attr_str = ''.join(' %s="%s"' % (attr, value)
                           for attr, value
                           in sorted(attrs.items()))
    else:
        attr_str = ''
    if content:
        return '\n'.join('<%s%s>%s</%s>' %
                         (name, attr_str, c, name)
                         for c in content)
    else:
        return '<%s%s />' % (name, attr_str)

if __name__ == '__main__':
    a = tag('br')
    print(a)
    a = tag('p', 'hello')
    print(a)
    print(tag('p', 'hello', 'world'))
    a = tag('p', 'hello', id=33)
    print(tag('p', 'hello', 'world', cls='sidebar'))
    a = tag(content='testing', name="img")
    print(a)
    my_tag = {'name': 'img', 'title': 'Sunset Boulevard',
              'src': 'sunset.jpg', 'cls': 'framed'}
    a = tag(**my_tag)
    print(a)